/**
  ******************************************************************************
  * @author     Chris
  * @since      2024/6/24 21:20
  *
  * @file       hd_24cxx.c
  * @brief      24CXX hardware driver.
  *
  * @note       This file contains the hardware driver for the 24CXX.
  *
  * @warning    None.
  ******************************************************************************
  * Change Logs:
  *   Date          Author       Notes
  *   2024/6/24     Chris        the first version
  *
  ******************************************************************************
  */

#include "hd_24cxx.h"

static void AT24CXX_init(AT24CXX *this) {

}

static void AT24CXX_read(AT24CXX *this, uint16_t address, uint8_t *dat, uint8_t len) {
    this->i2c->read(this->i2c, AT24CXX_ADDRESS, address, dat, len);
}

static void AT24CXX_write(AT24CXX *this, uint16_t address, uint8_t *dat, uint8_t len) {
    // TODO The 256K EEPROM are capable of 64-byte page write.
    this->i2c->send(this->i2c, AT24CXX_ADDRESS, address, dat, len);
}

static AT24CXX *build(I2C *i2c) {
    AT24CXX *at24cxx = malloc(sizeof(AT24CXX));

    Driver_add(at24cxx, GEN_PERM);

    at24cxx->i2c   = i2c;
    at24cxx->init  = &AT24CXX_init;
    at24cxx->read  = &AT24CXX_read;
    at24cxx->write = &AT24CXX_write;

    return at24cxx;
}

const struct AT24CXXClass AT24CXXBuilder = {.build = &build};
